package com.sxg.controller;

import com.sxg.convert.SxgCommentConvert;
import com.sxg.entity.SxgCommentEntity;
import com.sxg.query.SxgCommentChildQuery;
import com.sxg.query.SxgCommentQuery;
import com.sxg.utils.code.AjaxResult;
import com.sxg.vo.SxgCommentVO;
import com.sxg.vo.req.CommentReqVo;
import lombok.AllArgsConstructor;
import com.sxg.page.PageResult;
import com.sxg.utils.code.Result;
import com.sxg.service.SxgCommentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.List;

/**
 * 评论表
 *
 * @author 浪漫写尽歌谱 3298751420@qq.com
 * @since 1.0.0 2024-03-07
 */
@RestController
@RequestMapping("sxg/sxg_comment")
@AllArgsConstructor
public class SxgCommentController {
    private final SxgCommentService sxgCommentService;

    @GetMapping("page")
    public AjaxResult page(@Valid SxgCommentQuery query) {
        return sxgCommentService.page(query);
    }

    @GetMapping("child")
    public AjaxResult child(@Valid SxgCommentChildQuery query) {
        return sxgCommentService.childe(query);
    }

    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('sxg:sxg_comment:info')")
    public Result<SxgCommentVO> get(@PathVariable("id") Long id) {
        SxgCommentEntity entity = sxgCommentService.getById(id);

        return Result.ok(SxgCommentConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    public AjaxResult save(@RequestBody CommentReqVo vo) {
        return sxgCommentService.save(vo);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('sxg:sxg_comment:update')")
    public Result<String> update(@RequestBody @Valid SxgCommentVO vo) {
        sxgCommentService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @PreAuthorize("hasAuthority('sxg:sxg_comment:delete')")
    public Result<String> delete(@RequestBody List<Long> idList) {
        sxgCommentService.delete(idList);

        return Result.ok();
    }


}